﻿using System;
using System.Workflow.Activities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;

namespace TPG.SharePoint.Workflow.Sample
{
	public sealed partial class Workflow: SequentialWorkflowActivity
	{
        public string CustomTextData;
        public Guid TaskId = Guid.NewGuid();
        public Guid TaskId2 = Guid.NewGuid();

        public SampleConfiguration _configData;

		public Workflow()
		{
            SPWeb Web;
            
			InitializeComponent();
		}

        private void WorkflowActivated_Invoked(object sender, ExternalDataEventArgs e)
        {
            SPActivationEventArgs args = e as SPActivationEventArgs;

            _configData = 
                WorkflowDataSerializer.DeserializeFromXmlString<SampleConfiguration>(
                    args.properties.AssociationData);
        }

        private void CreateTaskOne_Invoking(object sender, EventArgs e)
        {
            CreateTask activity = sender as CreateTask;
           
            activity.TaskProperties = new SPWorkflowTaskProperties();
            activity.TaskProperties.AssignedTo = _configData.Level1Approvers[0];
            activity.TaskProperties.TaskType = 0;
            activity.TaskProperties.DueDate = DateTime.Now.AddDays(3);
            activity.TaskProperties.ExtendedProperties["CustomText"] = _configData.Instructions;
            activity.TaskProperties.Title = "Perform Task One";            
        }


        private void ChangeTaskOne_Invoked(object sender, ExternalDataEventArgs e)
        {
            SPTaskServiceEventArgs args = e as SPTaskServiceEventArgs;
            CustomTextData = args.afterProperties.ExtendedProperties["CustomText"] as string;
        }
        

        private void CreateTaskTwo_Invoking(object sender, EventArgs e)
        {
            CreateTask activity = sender as CreateTask;

            activity.TaskProperties = new SPWorkflowTaskProperties();
            activity.TaskProperties.AssignedTo = _configData.Level1Approvers[0];
            activity.TaskProperties.TaskType = 1;
            activity.TaskProperties.ExtendedProperties["CustomText"] = _configData.Instructions;
            activity.TaskProperties.Title = "Perform Task Two";
        }

        private void ChangeTaskTwo_Invoked(object sender, ExternalDataEventArgs e)
        {
            SPTaskServiceEventArgs args = e as SPTaskServiceEventArgs;
            _configData.Instructions = args.afterProperties.ExtendedProperties["CustomText"] as string;
        }

        private void WorkflowModification_Invoking(object sender, EventArgs e)
        {
            EnableWorkflowModification activity = sender as EnableWorkflowModification;

            InstructionsModification data = new InstructionsModification();
            data.Instructions = _configData.Instructions;

            ModificationData typedData = ModificationData.CreateUsingTypedData(data);
            activity.ContextData = typedData.NativeData;
        }
	}
}
